<?php include "checkSessionForAuthen.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Món Ăn</title>
        <?php include("import.php"); ?>
        <script type="text/javascript">
            function submitDelete(page_num) {
                var form = document.getElementById("form");
                form.method = "post";
                form.action = "../action/deleteDishAction.php?page_num=" + page_num;
                form.submit();
            }
            
            function confirmDelete(dishId, page_num) {
                if (confirm('Bạn có chắc muốn xóa không?')) {
                    window.location = "../action/deleteDishAction.php?id=" + dishId + "&page_num=" + page_num;
                }
            }
        </script>
    </head>

    <body>
        <form action="dish.php" method="get" id="form" name="form">
            <!--header-->
            <?php
            include "header.php";
            include_once("../service/dishService.php");
            include_once("../service/categoryService.php");

            /* process paging event */
            if (isset($_GET['page_num'])) {
                $page_num = $_GET['page_num'];
            } else {
                $page_num = 1;
            }

            $row_per_page = 10;

            $keyword = "";
            if (isset($_GET['keyword'])) {
                $keyword = $_GET['keyword'];
            }

            $total = countNumOfDishByName($keyword);
            $total_page = ceil($total / $row_per_page);
            $start_position = ($page_num * $row_per_page) - ($row_per_page);
            ?>
            <!--end header-->
            <!-- Container -->
            <div id="container">
                <div class="shell">

                    <?php
                    $message = "";
                    if (isset($_GET['message'])) {
                        $deleteMessage = $_GET['message'];
                    }
                    if (isset($deleteMessage)) {
                        if ($deleteMessage == 'deleted') {
                            $message = 'Đã xóa món ăn thành công';
                            $class = 'msg msg-ok';
                        } else if ($deleteMessage == 'nodeleted') {
                            $message = 'Không thể xóa món ăn';
                            $class = 'msg msg-error';
                        } else if ($deleteMessage == 'ok') {
                            $message = 'Cập nhật món ăn thành công';
                            $class = 'msg msg-ok';
                        } else if ($deleteMessage == 'assigned') {
                            $message = 'Món ăn có trong thực đơn';
                            $class = 'msg msg-error';
                        } else if ($deleteMessage == 'unique') {
                            $message = 'Món ăn đã tồn tại, vui lòng nhập thông tin khác!';
                            $class = 'msg msg-error';
                        }
                    }
					
                    if ($message == '') $class = 'msg msg-ok msg-hidden';
                    ?>	

                    <div class="<?php echo($class); ?>" id="msgOK">
                        <p><strong><?php echo($message); ?></strong></p>
                        <a href="#" onclick="hideMessage()" class="close">close</a>
                    </div>

                    <br />
                    <!-- Main -->
                    <div id="main">
                        <div class="cl">&nbsp;</div>

                        <!-- Content -->
                        <div id="content">

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2 class="left">Thực đơn</h2> 
                                    <div class="right">
                                        <label>Tìm Kiếm:</label>
                                        <input type="text" class="field small-field" name="keyword" />
                                    </div>
                                </div>
                                <!-- End Box Head -->	

                                <!-- Table -->
                                <div class="table" id="tblDish">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <th width="13"></th>
                                            <th>STT</th>
                                            <th>Loại thức ăn</th>
                                            <th>Món ăn</th>
                                            <th>Giá</th>
                                            <th>Mô tả</th>
                                            <th width="110" class="ac">Thao Tác</th>
                                        </tr>
                                        <?php
                                        $count = 1;

                                        $result = getDishesBy($keyword, $start_position, $row_per_page);

                                        while ($row = mysql_fetch_array($result)) {
                                            $cate = getCategoryById($row["CAT_ID"]);
                                            ?>
                                            <tr>
                                                <td><input type="checkbox" class="checkbox" value="<?php echo $row["DISH_ID"]; ?>" id="dishId[]" name="dishId[]" /></td>
                                                <td><?php echo $count; ?></td>
                                                <td><h3><?php echo $cate["CAT_NAME"]; ?></h3></td>
                                                <td><h3><a href="editDish.php?id=<?php echo $row["DISH_ID"]; ?>"><?php echo $row["DISH_NAME"]; ?></a></h3></td>
                                                <td><?php echo number_format($row["DISH_PRICE"]."000", 0, ",", "."); ?></td>
                                                <td><?php echo $row["DISH_DESCRIPTION"]; ?></td>
                                                <td><a href="#" class="ico del" onclick="confirmDelete(<?php echo $row["DISH_ID"] . "," . $page_num; ?>);">Delete</a>
                                                    <a href="editDish.php?id=<?php echo $row["DISH_ID"]; ?>" class="ico edit">Edit</a></td>

                                            </tr>
                                            <?php
                                            $count++;
                                        }
                                        ?>
                                    </table>


                                    <!-- Pagging -->
                                    <div class="pagging">
                                        <div class="left">Trang <?php echo(" " . $page_num . "  trong tổng số " . $total_page . " trang "); ?></div>
                                        <div class="right">
                                            <?php
                                            for ($i = 1; $i <= $total_page; $i++) {
                                                echo("<a href='" . "dish.php?" . "&keyword=" . $keyword . "&page_num=" . $i . "'>" . $i . "</a>");
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    <!-- End Pagging -->

                                </div>
                                <!-- Table -->

                            </div>
                            <!-- End Box -->

                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2>Thao tác</h2>
                                </div>
                                <!-- End ofBox Head -->
                                <div class="box-content">
                                    <div class="buttons">
                                        <input type="button" class="button" value="Xóa Món Ăn" id="deleteDish" name="deleteDish" onclick="if(validateDeletingFunction('tblDish', 'dishId[]', 'msgOK')) submitDelete(<?php echo $page_num; ?>);"/>&nbsp;
                                        <input type="button" class="button" value="Thêm Món Ăn" id="addDish" name="addDish" onclick="window.location = 'addDish.php'"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- End Content -->			
                        <div class="cl">&nbsp;</div>			
                    </div>
                    <!-- Main -->
                </div>
            </div>
            <!-- End Container -->

            <?php
            if (isset($_POST['addDish'])) {
                header("location:dishDetail.php");
            }
            ?>
            <!--footer-->
            <?php include "footer.php"; ?>
            <!--end footer-->
<?php ConnectionManager::getInstance()->closeConnection(); ?>
        </form>
    </body>
</html>